package com.usian.comment.service.impl;

import com.usian.comment.service.ApCommentLikeService;
import com.usian.model.comment.ApComment;
import com.usian.model.comment.ApCommentLike;
import com.usian.model.comment.dto.CommentLikeDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ApCommentLikeServiceImpl implements ApCommentLikeService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ResponseResult saveLike(CommentLikeDto dto) {

        if (dto==null || StringUtils.isBlank(dto.getCommentId()) || dto.getOperation()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //apcomment
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        if (apComment!=null){
            //点赞  apcomment like +1  insert apcommentlike
            if (dto.getOperation()==0){
                //apComment.setLikes(apComment.getLikes()+1);
                Update update=new Update();
                update.set("likes",apComment.getLikes()+1);
                mongoTemplate.upsert(Query.query(Criteria.where("id").is(dto.getCommentId())),update,ApComment.class);

                ApCommentLike apCommentLike=new ApCommentLike();
                apCommentLike.setAuthorId(apUser.getId());
                apCommentLike.setCommentId(dto.getCommentId());
                apCommentLike.setOperation(dto.getOperation());
                mongoTemplate.insert(apCommentLike);
            }else if (dto.getOperation()==1){
                //取消点赞 apcomment like -1  remove apcommentlike
                Update update = new Update();
                update.set("likes",apComment.getLikes()<=0? 0 : apComment.getLikes()-1);
                mongoTemplate.upsert(Query.query(Criteria.where("id").is(dto.getCommentId())),update,ApComment.class);

                mongoTemplate.remove(Query.query(Criteria.where("authorId").is(apUser.getId()).and("commentId")
                        .is(dto.getCommentId())),ApCommentLike.class);
            }
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //返回 like 数量
        Map<String,Object> result=new HashMap<>();
        ApComment apComment1 = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        result.put("likes",apComment1.getLikes());
        return ResponseResult.okResult(result);
    }
}
